package com.icezzz.code_backup;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;

public class CodeBackup {

    private static final List<String> ext = StrUtil.splitTrim(".java,.xml,.js,.properties,.yuml,.yml,.json,.txt", ",");

    public static void backup(String folder, Map<String, String> map) {
        boolean md5 = bool(map, "md5", false);
        boolean metadata = bool(map, "metadata", false);
        // boolean java = bool(map, "java", false);
        String path = StrUtil.replace(folder, "\\", "/");
        path = StrUtil.removeSuffix(path, "/");
        String left = StrUtil.subBefore(path, "/", true);
        String right = StrUtil.subAfter(path, "/", true);
        String subPath = right + "_";
        String newPath = left + "/" + subPath;
        codeReplace(path, newPath, map, md5, metadata, ext);
    }

    private static void codeReplace(String oldPath, String newPath, Map<String, String> map, boolean md5,
            boolean metadata, List<String> ext) {
        System.out.println("oldPath=" + oldPath);
        System.out.println("newPath=" + newPath);
        File oldFile = new File(oldPath);
        codeReplace(oldPath, newPath, oldFile, map, md5, metadata, ext);
    }

    private static void codeReplace(String oldPath, String newPath, File oldFile, Map<String, String> map, boolean md5,
            boolean metadata, List<String> ext) {
        if (oldFile.isDirectory()) {
            File[] listFiles = oldFile.listFiles();
            for (File subFile : listFiles) {
                codeReplace(oldPath, newPath, subFile, map, md5, metadata, ext);
            }
        } else {
            String absolutePath = oldFile.getAbsolutePath();
            absolutePath = absolutePath.replace("\\", "/");
            String subPath = StrUtil.removePrefix(absolutePath, oldPath);
            subPath = StrUtil.removePrefix(subPath, "/");
            String newPathAll = newPath + "/" + subPath;
            copyFile(absolutePath, newPathAll, subPath, map, md5, metadata, ext);
        }

    }

    private static void copyFile(String oldPath, String newPath, String subPath, Map<String, String> map, boolean md5,
            boolean metadata, List<String> ext) {
        String after = StrUtil.subAfter(oldPath, "/", true);
        String extension = StrUtil.subAfter(after, ".", true);
        String extensionFlag = "." + extension;
        subPath = "/" + subPath;
        if (!metadata) {
            if (subPath.contains("/.metadata/.plugins")) {
                return;
            }
            if (subPath.contains("/.metadata/.mylyn")) {
                return;
            }
            if (".lock".equals(after) && subPath.contains("/.metadata")) {
                return;
            }
            if ("version.ini".equals(after) && subPath.contains("/.metadata")) {
                return;
            }
        }
        if (".log".equalsIgnoreCase(extensionFlag)) {
            return;
        }
        if (subPath.contains("/target/classes/")) {
            return;
        }
        if (subPath.contains("/target/test-classes")) {
            return;
        }
        if (StrUtil.isBlank(extension) || ext.contains(extensionFlag)) {
            FileUtil.copy(oldPath, newPath, true);
        } else if (md5) {
            String md5Str = SecureUtil.md5(oldPath);
            String content = "UUUU_" + md5Str;
            FileUtil.writeUtf8String(content, newPath);
        }
    }

    public static boolean bool(Map<String, String> map, String key, boolean defualt) {
        String value = map.get(key);
        if (value == null || "".equals(value)) {
            return defualt;
        } else {
            return Objects.equals(true, value);
        }
    }

}
